package com.wl.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wl.connonutils.JwtUtils;
import com.wl.connonutils.MD5;
import com.wl.servicebase.exception.GuliException;
import com.wl.ucenter.entity.Member;
import com.wl.ucenter.entity.vo.RegistVo;
import com.wl.ucenter.mapper.MemberMapper;
import com.wl.ucenter.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author wl
 * @since 2021-09-02
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String login(Member member) {
        String mobile = member.getMobile();
        String password = member.getPassword();
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"登录失败");
        }
        password = MD5.encrypt(password);
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Member::getMobile,mobile);
        Member one = this.getOne(wrapper);
        if(null == one){
            throw new GuliException(20001,"手机号不存在");
        }
        if(!password.equals(one.getPassword())){
            throw new GuliException(20001,"登录失败");
        }
        if(one.getIsDisabled()){
            throw new GuliException(20001,"登录失败");
        }
        String jwtToken = JwtUtils.getJwtToken(one.getId(), one.getNickname());
        return jwtToken;
    }

    @Override
    public void register(RegistVo registVo) {
        String code = registVo.getCode();
        String mobile = registVo.getMobile();
        String nickname = registVo.getNickname();
        String password = registVo.getPassword();
        //非空判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password) || StringUtils.isEmpty(code)
            || StringUtils.isEmpty(nickname)){
            throw new GuliException(20001,"注册失败");
        }
        //判断验证码
        //获取redis验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(redisCode)){
            throw new GuliException(20001,"注册失败");
        }
        //判断手机号是否重复
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        int count = this.count(wrapper);
        if(count>0){
            throw new GuliException(20001,"注册失败");
        }
        Member member = new Member();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        member.setGmtCreate(new Date());
        member.setGmtModified(new Date());
        //数据添加数据库中
        this.save(member);
    }

    @Override
    public Member getOpenIdMember(String openid) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        Member member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer coutRegister(String day) {
        return baseMapper.coutRegister(day);
    }
}
